package cn.ilikes.tools.nosql.jpa.clients.indexs.index.hadoops;

import java.io.IOException;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.lucene.store.BufferedIndexOutput;

public class FileSystemIndexOutput extends BufferedIndexOutput {

	private FSDataOutputStream fsdataOutputStream = null;
	
	private Path pathfile;

	public FileSystemIndexOutput(FileSystem fileSystem, Path path, int ioFileBufferSize) throws IOException {
	          
		FSDataOutputStream out = fileSystem.append(path, ioFileBufferSize);
		setFsdataOutputStream(out);
		pathfile=path;
		

	}

	private void setFsdataOutputStream(FSDataOutputStream fsdataOutputStream) {
		
		this.fsdataOutputStream = fsdataOutputStream;
	}

	@Override
	protected void flushBuffer(byte[] b, int offset, int len) throws IOException {
		try{
	
		fsdataOutputStream.write(b, offset, len);
		fsdataOutputStream.flush();
		}catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			fsdataOutputStream.close();
		}

	}

	@Override
	public long length() throws IOException {
		return fsdataOutputStream.getPos();
	}

	

}
